﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using ExtremeML.Packaging;

namespace ExtremeML.Spreadsheet
{
	// This is a partial class that adds custom functionality to its auto-generated partner.
	public sealed partial class ColumnWrapperCollection
	{
		#region Custom Properties

		/// <summary>
		/// Gets the containing <see cref="WorkbookPartWrapper"/>.
		/// </summary>
		public WorkbookPartWrapper WorkbookPart
		{
			get { return Worksheet.WorksheetPart.WorkbookPart; }
		}
	
		/// <summary>
		/// Gets the containing <see cref="WorksheetWrapper"/>.
		/// </summary>
		public WorksheetWrapper Worksheet
		{
			get { return (WorksheetWrapper)Parent.Parent.Parent; }
		}

		#endregion
		#region Custom Methods

		/// <summary>
		/// Returns the wrapper for the column having the specified index.
		/// </summary>
		/// <param name="columnIndex">The zero-based index of the target column.</param>
		/// <returns>A <see cref="ColumnWrapper"/> that matches the specified index. <c>null</c> if no match is found.</returns>
		public ColumnWrapper Find(
			int columnIndex)
		{
			return Items.Find(item => item.Min <= columnIndex + 1 && item.Max >= columnIndex + 1);
		}

		#endregion
		#region Overrides

		/// <summary>
		/// Performs housekeeping and validation of a requested deletion.
		/// </summary>
		protected override void OnCustomRemoved(
			ColumnWrapper element)
		{
			if (Count == 0)
			{
				// Since the schema prohibits empty Columns elements, deletion
				// of the last Column element requires deletion of its container.
				((ColumnsWrapper)Parent).Remove();
			}
		}

		#endregion
		#region Implementation Methods


		#endregion
		#region Static Methods


		#endregion
	}
}
